<template>
    <div>
        <h2>新增页面</h2>

        <table class="table">
            <tbody>
                <tr>
                    <td>名称</td>
                    <td>
                        <input type="text" v-model="data.Ename">
                    </td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" v-model="data.Esex" :value="1">男
                        <input type="radio" v-model="data.Esex" :value="2">女
                    </td>
                </tr>
                <tr>
                    <td>生日</td>
                    <td>
                        <input type="date" v-model="data.Ebir">
                    </td>
                </tr>
                <tr>
                    <td>手机号</td>
                    <td>
                        <input type="text" v-model="data.Ephone">
                    </td>
                </tr>
                <tr>
                    <td>年龄</td>
                    <td>
                        <input type="text" v-model="data.Eage">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="checkbox" v-model="data.Etype">选中启用
                    </td>
                </tr>
                <tr>
                    <td>会员等级</td>
                    <td>
                        <select v-model="data.MemberLevelId">
                            <option value="0">请选择</option>
                            <option v-for="a in lala" :value="a.Mid">{{ a.Mname }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file"  @change="upload">
                        <img :src="'https://localhost:7072'+img" style="width: 100px; height: 100px;">
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="button" value="新增" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">

import axios from 'axios';
import { ref,onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router=useRouter();

const data=ref({
  "Eid": 0,
  "Ename": "",
  "Esex": 0,
  "Ebir": "2025-04-05T11:53:34.931Z",
  "Ephone": "",
  "Eage": 0,
  "IsDelete": false,
  "Edate": "2025-04-05T11:53:34.931Z",
  "Etype": false,
  "MemberLevelId": 0,
  "Epic": ""
})

const lala=ref([{
    "Mid": 0,
    "Mname": ""
}])

const add=()=>{
    axios.post("https://localhost:7072/api/Emp/Add",data.value).then(res=>{
        if(res.data>0)
        {
            alert("新增成功");
            router.push({path:"/"})
        }
        else
        {
            alert("新增失败");
        }
    })
}

const img=ref("")

const upload=(e:any)=>{
    var p=e.target.files[0];
    var fg=new FormData();
    fg.append("file",p);
    debugger

    if(p.size>2*1024*1024)
    {
        alert("上传的图片不能大于2KB");
        return;
    }

    if(p.type!="image/jpeg" && p.type!="image/png")
    {
        alert("只能上传jpg或png的图片");
        return;
    }

    axios.post("https://localhost:7072/api/Emp/Upload",fg).then(res=>{
        img.value=res.data
        data.value.Epic=res.data

        alert("上传成功");
    })
}

const la=()=>{
    axios.get("https://localhost:7072/api/Emp/Mlist").then(res=>{
        lala.value=res.data
    })
}

onMounted(() => {
    la()
})

</script>

<style scoped>

</style>